package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.DepartmentService;
import cn.wolfcode.vo.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 部门处理器
 *
 * @author 杨兴健
 * @Date 2020/7/9 10:30
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService service;

    /**
     * 查询所有部门
     *
     * @param qo
     * @param model
     * @return
     */
    @RequiresPermissions(value = {"department:list", "部门查询"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        model.addAttribute("pageInfo", service.selectAllByQO(qo));
        return "/department/list";
    }

    /**
     * 跳转跟新|添加部门页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions(value = {"department:input", "部门更新|添加页面"}, logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            Department department = service.selectById(id);
            model.addAttribute("department", department);
        }
        return "/department/input";
    }

    /**
     * 更新|添加部门
     *
     * @param department
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"department:saveOrUpdate", "部门更新|添加"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {

        service.saveOrUpdate(department);
        return new JsonResult(true, "操作成功");
    }

    /**
     * 根据id删除部门
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @RequiresPermissions(value = {"department:delete", "部门删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        service.delete(id);
        return new JsonResult(true, "删除成功！");
    }

    /**
     * 获取所有的部门
     *
     * @return
     */
    @RequestMapping("/getDept")
    @ResponseBody
    public List<Department> getDept() {
        return service.selectAll();
    }
}
